package classesMiolo;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class XmlEditor {
    private String username;
    private String password;
    private String serverIP;
    
    public XmlEditor(String serverIP){
        this.serverIP = serverIP;
        this.username = "postgres";
        this.password = "08041993";
    }
    
    public XmlEditor(String serverIP, String user, String password){
        this.password = password;
        this.serverIP = serverIP;
        this.username = user;
    }
    
    private String mountString(){
        StringBuilder sb = new StringBuilder("");
        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        sb.append("<persistence version=\"1.0\" xmlns=\"http://java.sun.com/xml/ns/persistence\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd\">\n");
        sb.append("  <persistence-unit name=\"Juliano_MoreiraPU\" transaction-type=\"RESOURCE_LOCAL\">\n");
        sb.append("    <provider>org.hibernate.ejb.HibernatePersistence</provider>\n");
        sb.append("    <class>classesBancodeDados.FichaHistEnfermagem</class>\n");
        sb.append("    <class>classesBancodeDados.FichaInternamento</class>\n");
        sb.append("    <class>classesBancodeDados.Responsavel</class>\n");
        sb.append("    <class>classesBancodeDados.FichaEncInternHospPsiq</class>\n");
        sb.append("    <class>classesBancodeDados.FichaAdmissaoMedica</class>\n");
        sb.append("    <properties>\n");
        sb.append("      <property name=\"hibernate.connection.username\" value=\"" + username + "\"/>\n");
        sb.append("      <property name=\"hibernate.connection.driver_class\" value=\"org.postgresql.Driver\"/>\n");
        sb.append("      <property name=\"hibernate.connection.password\" value=\"" + password + "\"/>\n");
        sb.append("      <property name=\"hibernate.connection.url\" value=\"jdbc:postgresql://" + serverIP + ":5432/julianomoreira\"/>\n");
        sb.append("      <property name=\"hibernate.cache.provider_class\" value=\"org.hibernate.cache.NoCacheProvider\"/>\n");
        sb.append("      <property name=\"hibernate.hbm2ddl.auto\" value=\"update\"/>\n");
        sb.append("    </properties>\n");
        sb.append("  </persistence-unit>\n");
        sb.append("</persistence>");
        return sb.toString();
    }
    
    public void writeFile(){
        String fname = ".\\build\\classes\\META-INF\\persistence.xml";
        String fname2 = ".\\src\\META-INF\\persistence.xml";
        if(System.getProperty("os.name").equals("Linux")){
            fname = "./build/classes/META-INF/persistence.xml";
            fname2 = "./src/META-INF/persistence.xml";
        }//
        try{
            BufferedWriter bw = new BufferedWriter(new FileWriter(new File(fname), false));
            BufferedWriter bw2 = new BufferedWriter(new FileWriter(new File(fname2), false));
            String texto = this.mountString();
            bw.write(texto);
            bw2.write(texto);
            bw.close();
            bw2.close();
        }catch(IOException e){
            System.err.println("Falha na abertura do arquivo persistence.xml");
        }
    }

    public static void main(String[] args){
        XmlEditor xe = new XmlEditor("150.165.130.14", "postgres", "08041993");
        xe.writeFile();
    }

}